package com.zggk.lzz.xcbgadd;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.bigkoo.pickerview.listener.CustomListener;
import com.blankj.utilcode.util.ToastUtils;
import com.zggk.lzz.R;
import com.zggk.lzz.adapter.LGRecycleViewAdapter;
import com.zggk.lzz.adapter.LGViewHolder;
import com.zggk.lzz.api.MyApplication;
import com.zggk.lzz.gj.GJActivity;
import com.zggk.lzz.mvp.MVPBaseActivity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class XcbgAddActivity extends MVPBaseActivity<XcbgAddContract.View, XcbgAddPresenter> implements XcbgAddContract.View {

    @Bind(R.id.back)
    ImageView back;
    @Bind(R.id.title_text)
    TextView titleText;
    @Bind(R.id.cancle_button)
    TextView cancleButton;
    @Bind(R.id.xcry_te)
    TextView xcryTe;
    @Bind(R.id.xcrq_te)
    TextView xcrqTe;
    @Bind(R.id.xcys_te)
    TextView xcysTe;
    @Bind(R.id.xz_te)
    TextView xzTe;
    @Bind(R.id.xcfw_re)
    RecyclerView xcfwRe;
    @Bind(R.id.xcjl_re)
    RecyclerView xcjlRe;
    @Bind(R.id.bcsm_et)
    EditText bcsmEt;
    @Bind(R.id.yy_img)
    ImageView yyImg;
    @Bind(R.id.xclc_te)
    TextView xclcTe;
    @Bind(R.id.xcgj_te)
    TextView xcgjTe;
    @Bind(R.id.save_te)
    TextView saveTe;
    @Bind(R.id.activity_disease_new_scrollview)
    ScrollView activityDiseaseNewScrollview;
    @Bind(R.id.disease_new_parent_layout)
    RelativeLayout diseaseNewParentLayout;
    @Bind(R.id.gj_te)
    TextView gjTe;
    private LGRecycleViewAdapter<String> adapter;
    private List<String> XcfwDatas = new ArrayList<>();
    private List<XcbgAddBean.DataBean.XcjlBean> XcjlDatas = new ArrayList<>();
    private LGRecycleViewAdapter<XcbgAddBean.DataBean.XcjlBean> adapter2;
    private OptionsPickerView pickerView;
    private List<String> roadDatas = new ArrayList<>();
    private String ID;

    @Override
    public void onRequestError(String msg) {

    }

    @Override
    protected int getLayout() {
        return R.layout.act_xcbg_add;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        initView();
        initAdapter();
        initData();
    }

    private void initData() {
        mPresenter.getData(XcbgAddActivity.this);
        mPresenter.addRoad();
    }

    private void initAdapter() {
        xcfwRe.setHasFixedSize(true);
        xcfwRe.setNestedScrollingEnabled(false);
        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        xcfwRe.setLayoutManager(manager);
        adapter = new LGRecycleViewAdapter<String>(XcfwDatas) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_xcfw;
            }

            @Override
            public void convert(final LGViewHolder holder, String zCDBean, final int position) {
                holder.setText(R.id.lxbm_te, zCDBean);
                holder.getView(R.id.de_img).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (!XcfwDatas.get(holder.getAdapterPosition()).equals("无法自动获取路线，请手动添加")){
                            XcfwDatas.remove(holder.getAdapterPosition());
                            if (XcfwDatas.size()==0){
                                XcfwDatas.add("无法自动获取路线，请手动添加");
                            }
                            adapter.notifyDataSetChanged();
                        }
                    }
                });
            }
        };
        xcfwRe.setAdapter(adapter);
        xcjlRe.setHasFixedSize(true);
        xcjlRe.setNestedScrollingEnabled(false);
        LinearLayoutManager manager2 = new LinearLayoutManager(getContext());
        manager2.setOrientation(LinearLayoutManager.VERTICAL);
        xcjlRe.setLayoutManager(manager2);
        adapter2 = new LGRecycleViewAdapter<XcbgAddBean.DataBean.XcjlBean>(XcjlDatas) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.item_xcjl2;
            }

            @Override
            public void convert(LGViewHolder holder, XcbgAddBean.DataBean.XcjlBean zCDBean, int position) {
                holder.setText(R.id.lxbm_te, zCDBean.getResult());
            }
        };
        xcjlRe.setAdapter(adapter2);
    }

    private void initView() {
        setTitleText("巡查报告");
        goBack();
        xcryTe.setText(MyApplication.spUtils.getString("OrgName"));
        xcrqTe.setText(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
    }

    @OnClick({R.id.save_te, R.id.xz_te, R.id.gj_te})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.gj_te:
                Intent intent = new Intent(XcbgAddActivity.this, GJActivity.class);
                intent.putExtra("id", ID);
                intent.putExtra("time", xcrqTe.getText().toString());
                intent.putExtra("xcr", xcryTe.getText().toString());
                startActivity(intent);
                break;
            case R.id.save_te:
                String roadStr = "";
                if (XcfwDatas != null && XcfwDatas.size() > 0) {
                    for (int i = 0; i < XcfwDatas.size(); i++) {
                        if (i == 0) {
                            roadStr += XcfwDatas.get(i);
                        } else {
                            roadStr += "," + XcfwDatas.get(i);
                        }
                    }
                }
                mPresenter.addData(bcsmEt.getText().toString(), roadStr, XcbgAddActivity.this);
                break;
            case R.id.xz_te:
                pickerView = new OptionsPickerView.Builder(XcbgAddActivity.this, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int option2, int options3, View v) {
                        if (roadDatas.get(0).equals("无法自动获取路线，请手动添加")){
                            roadDatas.clear();
                        }
                        XcfwDatas.add(roadDatas.get(options1));
                        adapter.notifyDataSetChanged();
                    }
                }).setLayoutRes(R.layout.progress_lay, new CustomListener() {
                    @Override
                    public void customLayout(View v) {
                        final TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);
                        TextView ivCancel = (TextView) v.findViewById(R.id.iv_cancel);
                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pickerView.returnData();
                                pickerView.dismiss();
                            }
                        });
                        ivCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pickerView.dismiss();
                            }
                        });
                    }
                }).build();
                pickerView.setPicker(roadDatas);
                pickerView.show();

                break;
        }
    }

    @Override
    public void getDatas(XcbgAddBean.DataBean dataBean) {
        xcysTe.setText(dataBean.getStartTime() + "-" + dataBean.getEndTime());
        xclcTe.setText(dataBean.getXclc() + "km");
        if (dataBean.getXcfw() != null && dataBean.getXcfw().size() > 0) {
            for (int i = 0; i < dataBean.getXcfw().size(); i++) {
                XcfwDatas.add(dataBean.getXcfw().get(i));
            }
        }
        if (XcfwDatas.size()==0){
            XcfwDatas.add("无法自动获取路线，请手动添加");
        }
        adapter.notifyDataSetChanged();
        if (dataBean.getXcjl() != null && dataBean.getXcjl().size() > 0) {
            for (int i = 0; i < dataBean.getXcjl().size(); i++) {
                XcjlDatas.add(dataBean.getXcjl().get(i));
            }
        }
        if (XcjlDatas.size()==0){
            XcbgAddBean.DataBean.XcjlBean xcjlBean =new  XcbgAddBean.DataBean.XcjlBean();
            xcjlBean.setResult("本次巡查未获取到相关上报事件");
            XcjlDatas.add(xcjlBean);
        }
        adapter2.notifyDataSetChanged();
        ID = dataBean.getPatrolId();
    }

    @Override
    public void addDatas() {
        ToastUtils.showShort("提交成功");
        finish();
    }

    @Override
    public void addRoads(XcbgRoadBean xcbgRoadBean) {
        for (int i = 0; i < xcbgRoadBean.getData().getRecords().size(); i++) {
            roadDatas.add(xcbgRoadBean.getData().getRecords().get(i));
        }
    }
}
